package com.gym.myshop.services.impl;

import com.gym.myshop.common.Const;
import com.gym.myshop.dao.UserDao;
import com.gym.myshop.dao.impl.UserDaoImpl;
import com.gym.myshop.entity.User;
import com.gym.myshop.services.UserServices;
import com.gym.myshop.util.*;


/**
 * @author ming
 * 2021/11/4
 * description:实现UserServices登录，注册，激活接口方法
 */
public class UserServicesImpl implements UserServices {
    private UserDao ud = new UserDaoImpl();


    /**
     *@author:ming
     *@Date: 2021/11/4
     *@description:用户名查用户是否存在
     */
    @Override
    public User checkName(String username) {

        try {
            DBUtil.begin();
            //比较用户名,非空判断
            if (StrUtils.isNotEmpty(username)) {
                User u = ud.getUserByName(username);
                DBUtil.commit();
                if (u != null) {
                    return u;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            DBUtil.rollback();
        }
        return null;
    }


    /**
     *@author:ming
     *@Date: 2021/11/5
     *@description:邮件激活zhuce
     */
    @Override
    public boolean addUser(User u) {

        boolean flag = false;
        if (u!=null) {
            try {
                DBUtil.begin();
                int i =  ud.addUser(u);
                DBUtil.commit();
                if (i>0) {
                    //调用发送邮件,等待太慢使用线程发送邮件
                    new  Thread(()-> EmailUtils.sendEmail(u)).start();
                    flag = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
                DBUtil.rollback();
            }
        }
        return flag;
    }

    /**
     *@author:ming
     *@Date: 2021/11/5
     *@description:登录,不需要dao层，在sercice完成
     */
    @Override
    public User login(String username, String password) {
        User flag = null;
        try {
            DBUtil.begin();
            if (StrUtils.isNotEmpty(username, password)) {
                User user = ud.getUserByName(username);
                //提交事务
                DBUtil.commit();
                //验证是否查询到用户数据及用户密码是否正确,并加密密码
                if (user != null && user.getUpassword().equals(MD5Utils.md5(password))) {
                    flag = user;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            //回滚
            DBUtil.rollback();
        }
        return flag;
    }

    /**
     *@author:ming
     *@Date: 2021/11/5
     *@description:邮件激活
     */
    @Override
    public String activate(String e, String c) {

        try {
            DBUtil.begin();
            //1 用户不存在
            User u = ud.getUserByEmailAndCode(Base64Utils.decode(e), Base64Utils.decode(c));
            DBUtil.commit();
            if (u == null) {
                return "该用户失败激活";
            }

            //2 用户已经激活过了
            if (u.getUstatus() == Const.UserConst.ACTIVATE) {
                return "该用户已激活";
            }
            DBUtil.begin();
            //3 正常激活成功
            int i = ud.updateUserStatus(Const.UserConst.ACTIVATE, u.getUid());
            DBUtil.commit();
            if (i > 0) {
                return "激活成功";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            DBUtil.rollback();
        }
        return "激活失败";
    }
}
